package view;

import java.util.List;

import org.apache.catalina.Session;
import org.apache.struts2.interceptor.SessionAware;
 
import controller.ContactManager;
import model.Contact;
 
import com.opensymphony.xwork2.ActionSupport;
 
 
@SuppressWarnings("unused")
public class ContactAction extends ActionSupport {
 
    private static final long serialVersionUID = 9149826260758390091L;
    private Contact contact;
    private List<Contact> contactList;
    private Long id;
    private ContactManager contactManager;
 
    public ContactAction() {
        contactManager = new ContactManager();
    }
 
    public String execute() {
     try {
    	this.contactList = contactManager.list();
        System.out.println("execute called");
     }    
     catch (Exception e) {
        e.printStackTrace();
        return ERROR;
    }
        return SUCCESS;
    }
 
    public String add() {
        System.out.println(getContact());
        try {
           contact = contactManager.add(getContact());
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }
 
    public String delete() {
    	try {
    		contact = contactManager.delete(getId());
    	} catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }
    

    public String load() {
    	try {
    		contact = contactManager.load(getId());
    	} catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    	return SUCCESS;
    }
    
    
    public String closeCon() {
    	try {
    		this.contactManager.closeSession();
        	} catch (Exception e) {
                e.printStackTrace();
                return ERROR;
            }
        	return SUCCESS;    	
    }
    
    public String update() {
    	//System.out.println(contact);
    	//this.contact.setId(getId());
    	try {
    		contact = contactManager.update(contact);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }
    
     
    public Contact getContact() {
        return contact;
    }
 
    public List<Contact> getContactList() {
        return contactList;
    }
 
    public void setContact(Contact contact) {
        this.contact = contact;
    }
 
    public void setContactList(List<Contact> contactsList) {
        this.contactList = contactsList;
    }
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
}